<?php
import( 'de.ceus-media.framework.krypton.core.View' );
import( 'de.ceus-media.adt.json.Builder' );

/**
 *	Views of User Interface.
 *	@package		mv2.view
 *	@extends		Framework_Krypton_Core_View
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			11.12.2006
 *	@version		0.1
 */
/**
 *	Views of User Interface.
 *	@package		mv2.view
 *	@extends		Framework_Krypton_Core_View
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			11.12.2006
 *	@version		0.1
 */
class View_Interface extends Framework_Krypton_Core_View
{
	/**
	 *	Builds Main Header View.
	 *	@access		public
	 *	@return		string
	 *	@author		Christian Würker <Christian.Wuerker@CeuS-Media.de>
	 *	@since		11.12.2006
	 *	@version	0.2
	 */
	public function buildMainHeader()
	{
		$words		= $this->registry->get( "words" );
		return $this->loadTemplate( 'header', $words['main']['header'] );
	}

	/**
	 *	Builds Sub Header View.
	 *	@access		public
	 *	@return		string
	 *	@author		Christian Würker <Christian.Wuerker@CeuS-Media.de>
	 *	@since		11.12.2006
	 *	@version	0.2
	 */
	public function buildSubHeader()
	{
		return "";
	}

	public function buildDevCenter( $content )
	{
		import( 'de.ceus-media.framework.krypton.view.component.DevCenter' );
		$view	= new Framework_Krypton_View_Component_DevCenter();
		return $view->buildContent( $content );
	}

	/**
	 *	Builds Main Navigation View.
	 *	@access		public
	 *	@return		string
	 *	@author		Christian Würker <Christian.Wuerker@CeuS-Media.de>
	 *	@since		11.12.2006
	 *	@version	0.9
	 */
	public function buildMainNavigation()
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$auth		= $this->registry->get( 'auth' );
		$controller	= $this->registry->get( 'controller' );
		
		$path		= "config/";		

		$roles		= array( "public" );
		if( $auth->isAuthenticated() )
		{
			$roles[]	= "inside";
			if( $auth->isTrader() )
				$roles[]	= "trader";
			if( $auth->isSeller() )
				$roles[]	= "seller";
			if( $auth->isLocationLeader() )
				$roles[]	= "location_leader";
			if( $auth->isCompanyLeader() )
				$roles[]	= "company_leader";
		}
		else
			$roles[]	= "outside";
		
		$roles	= array_map( create_function( '$role', 'return "roles/".$role;' ), $roles );
		$roles	= implode( " or ", $roles );

		//  --  LOAD XSL TRANSFORMATION DOCUMENT  --  //
		$xml		= $controller->getDocument();
		$xsl		= file_get_contents( file_exists( $path."navigation.xslt" ) ? $path."navigation.xslt" : "config/navigation.xslt" );
		$xsl		= str_replace( "{#roles#}", $roles, $xsl );
		$xsl		= DOMDocument::loadXML( $xsl );

		//  --  XSL TRANSFORMATION  --  //
		$proc		= new XSLTProcessor();
		$proc->importStyleSheet( $xsl );
		$result 	=  $proc->transformToXML( $xml );

		foreach( $this->words['main']['links_main'] as $linkKey => $linkLabel )
			$result	= str_replace( ">".$linkKey."<", ">__".$linkLabel."__<", $result );
		$result	= str_replace( "__", "", $result );

		$ui	= array(
			'link'		=> $request->get( 'link' ),
			'list'		=> $result,
			);
		$userId	= $auth->getCurrentUser();
		if( $userId )
		{
			$logic		= new Logic_User();
			$ui['username']	= $logic->getUsername( $userId );
		}
		return $this->loadTemplate( "navi", $ui );
	}

	/**
	 *	Builds Sub Navigation View.
	 *	@access		public
	 *	@return		string
	 *	@author		Christian Würker <Christian.Wuerker@CeuS-Media.de>
	 *	@since		11.12.2006
	 *	@version	0.9
	 */
	public function buildSubNavigation()
	{
		return "";
	}

	/**
	 *	Builds Main Footer View.
	 *	@access		public
	 *	@return		string
	 *	@author		Christian Würker <Christian.Wuerker@CeuS-Media.de>
	 *	@since		11.12.2006
	 *	@version	0.2
	 */
	public function buildMainFooter()
	{
		return "";
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$auth		= $this->registry->get( 'auth' );
		$controller	= $this->registry->get( 'controller' );

		$path		= "config/";
		
		$xml		= $controller->getDocument();
		$xsl		= file_get_contents( file_exists( $path."footer.xslt" ) ? $path."footer.xslt" : "config/footer.xslt" );
		$xsl		= DOMDocument::loadXML( $xsl );
		$proc		= new XSLTProcessor();
		$proc->importStyleSheet( $xsl );
		$result 	=  $proc->transformToXML( $xml );
		if( function_exists( 'json_encode' ) )
		{
			array_walk( $this->words['main']['links_footer'], create_function( '&$value', '$value = utf8_encode( $value );' ) );
			$json	= json_encode( $this->words['main']['links_footer'] );
		}
		else
		{
			import( 'de.ceus-media.adt.json.JSON_Builder' );
			$json	= new JSON_Builder();
			$json	= $json->encode( $this->words['main']['links_footer'] );
		}

		$ui	= array(
			'link'		=> $request->get( 'link' ),
			'list'		=> $result,
			'words'		=> $json,
			'config'	=> $config['application'],
			);
		return $this->loadTemplate( "mainfooter", $ui );
	}

	/**
	 *	Builds Sub Footer View.
	 *	@access		public
	 *	@return		string
	 *	@author		Christian Würker <Christian.Wuerker@CeuS-Media.de>
	 *	@since		11.12.2006
	 *	@version	0.2
	 */
	public function buildSubFooter()
	{
		$config		= Framework_Krypton_Core_Registry::getStatic( 'config' );
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$stopwatch	= Framework_Krypton_Core_Registry::getStatic( 'stopwatch' );
		$links		= $config['subfooter'];

		$content	= "";
		$list		= "";
		if( $links )
		{
			$list	= array();
			foreach( $links as $link )
			{
				$entry_style	= count( $list ) ? "" : "first";
				$entry_style	= ( count( $list ) == count( $links ) - 1 ) ? "last" : $entry_style;
				if( isset( $link['src'] ) && $link['src'] )
				{
					$entry	= $this->html->Image( "contents/images/badges/".$link['src'], $link['alt'] );
					if( isset( $link['url'] ) )
					{
						$target	= isset( $link['target'] ) ? $link['target'] : false;
						$entry	= $this->html->Link( $link['url'], $entry, 'image', $target );
					}
				}
				$style	= $entry_style ? " class='".$entry_style."'" : "";
				$list[]	= "<li".$style.">".$entry."</li>";
			}
			$list	= "<ul>".implode( "\n\t\t", $list )."</ul>";
		}
		$ui	= array(
			'link'		=> $request->get( 'link' ),
			'list'		=> $list,
			'config'	=> $config['application'],
			'time'		=> $stopwatch->stop( 0, 3 ),
			);
		return $this->loadTemplate( "subfooter", $ui );
	}
}
?>